即使启用了卷曲,Php CURL Post也无法正常工作

时间:2015-04-08 05:37:20

标签: php curl

我知道有很多相同的问题,我已经尝试了很多问题的解决方案,但我仍然无法解决这个问题。

我正在尝试将卷曲帖从一个服务器发送到另一个服务器,如此

$array = array("businessname" => "Illusion Softwares");
# try hitting the Tracking via CURL 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/testCurl.php");
curl_setopt($ch, CURLOPT_POST, count($array));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
$result = curl_exec($ch);
curl_close($ch);
echo $result;

这就是我在发布的服务器上的testCurl.php上的内容

echo $_REQUEST['businessname'];
exit;

当我运行页面时,它会一直加载并加载并加载超时错误消息。

我已在两台服务器上启用了curl。

我错过了什么?

3 个答案:

答案 0 :(得分:0)

添加此行,

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/testCurl.php");
    curl_setopt($ch, CURLOPT_POSTFIELDS, array("businessname" => "Illusion Softwares"));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;

答案 1 :(得分:0)

试试这个

$array = array("businessname" => "Illusion Softwares");
$headers = array(
            "Content-type: text/xml",
            "Content-length: " . strlen($array),
            "Connection: close",
        );

# try hitting the Tracking via CURL 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/testCurl.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 4000);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 $data = curl_exec($ch);
        if (curl_errno($ch)) {
            curl_error($ch);
            return FALSE;
        } else {
            curl_close($ch);
            return TRUE;

答案 2 :(得分:0)

此卷曲选项​​设置将为您提供找出问题所需的信息

您可能需要curl_setopt($ch, CURLOPT_FAILONERROR,true);

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_ENCODING,"");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT,100);
  curl_setopt($ch, CURLOPT_FAILONERROR,true);

然后你需要检查错误,如果没有错误,那么请查看请求和响应标头。下面我得到了响应标题,请求标题位于$ info。

  $data = curl_exec($ch);
  if (curl_errno($ch)){
      $data .= 'Retreive Base Page Error: ' . curl_error($ch);
      echo $data;
  }
  else {
    $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
    $responseHeader = substr($data,0,$skip);
    $data= substr($data,$skip);
    $info = curl_getinfo($ch);
    $info = var_export($info,true);
   }
   echo $responseHeader . $info . $data ;

如果您收到错误,则可能是请求有问题。

要自定义您的请求,请执行以下示例:

$request = array();
$request[] = 'Host: xxxxxxx';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'DNT: 1';
$request[] = 'Cookie: xxxx
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';

然后包括:

  curl_setopt($ch, CURLOPT_HTTPHEADER, $request);