douhuanchi6586 2012-07-10 18:08
浏览 43
已采纳

CURL仅在某个主机上的某些站点上返回任何内容

I have been using curl to get an xml response from a payment gateway since last january and before another one using the same code since about 5 years. Last friday it stopped working and the code hasn't been modified and both the host and the payment gateway tell me nothing has changed either.

I have done multiple tests to access the url with curl, fopen and file_get_contents to try to make it work. I seem to be able to access common sites like Google, Facebook and php.net but not the site I want and other sites i worked on.

The code I use for testing is :

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1); // comment once in production
if(!curl_setopt($ch, CURLOPT_URL, $url)) {echo 'CURLOPT URL Error<p>';}
if(!curl_setopt($ch, CURLOPT_HEADER, 1)) {echo 'CURLOPT Header Error<p>';}
if(!curl_setopt($ch, CURLOPT_HTTPHEADER, array('POST /AUTHORIZE HTTP/1.0', 'MIME-Version: 1.0', 'Content-type: application/PTI21',
'Content-transfer-encoding: text', 'Request-number: 1', 'Document-type: Request'))) {echo 'CURLOPT HTTPHEADER Error<p>';}
if(!curl_setopt($ch, CURLOPT_TIMEOUT, 90)) {echo 'CURLOPT TIMEOUT Error<p>';} 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // gives error, but keeps xml formatting

$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
$options = array(
    'return_info'   => true,
    'method'        => 'post'
);
$result = file_get_contents($url);
print_r($result);


$fd = fopen( $url, "r" );
if( !$fd )
{
    echo "Cannot open URL";
} else {
    while ( !feof( $fd ) ) {
        $buffer = fgets( $fd, 4096 );
        echo $buffer;
    }
    fclose ( $fd );
}

I was wondering if there was anything wrong with it because on another host it works everytime and with the host it's on, it only works for half the sites i try.

  • 写回答

1条回答 默认 最新

  • dongli8722 2012-07-10 18:25
    关注

    It's hard to give a valid response without knowing the actual URL.

    One problem could be that the URL is redirecting, for example when some people redirect domain.com to www.domain.com.

    For those scenarios you can use.

     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    

    Please run the command below to get some more information on what happened.

    print_r(curl_info($ch));
    

    Put that code right below the curl_exec

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线