PHP curl总是返回http_code 0

时间:2015-10-12 16:24:19

标签: php curl

Hello StackOverflow社区,我尝试在PHP上使用cURL方法时遇到了问题。我试过这个示例代码:

$html_brand = "www.google.com/";
$ch = curl_init();

$options = array(
    CURLOPT_URL            => $html_brand,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => true,
    CURLOPT_HTTPHEADER     => array("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"),
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_ENCODING       => "",
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120,
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ( $httpCode != 200 ){
    echo "Return code is {$httpCode} \n"
        .curl_error($ch);
} else {
    echo "<pre>".htmlspecialchars($response)."</pre>";
}

curl_close($ch);

它总是以错误结束,在屏幕上显示:

  

返回代码为0 Recv failure:连接已重置

或尝试使用https:

访问任何网站时出现此错误
  

返回代码为0无法连接到www.google.com端口443:定时   出

这是我的设置:

  • Windows 7 Professional 32位
  • Apache 2.4.12
  • PHP 5.6.11

是代码错误还是我未考虑过的任何服务器配置?

Apache中的HTTP_HOST值为localhost:8080,我不确定它是否与我的问题有关,但也许值得注意。

提前感谢大家。

1 个答案:

答案 0 :(得分:0)

我开发了一个自定义函数,适用于public void setElencoIdOperatori(int[] id_nome_op){ this.id_nome_op = id_nome_op; } 个请求。

所以这就是

    private void getMoreEvents(Calendar day){
// Get more events if the month is changed.
if(mEventRects ==null)
    mEventRects =newArrayList<EventRect>();

if(mMonthChangeListener ==null&&!isInEditMode())
    thrownewIllegalStateException("You must provide a MonthChangeListener");

// If a refresh was requested then reset some variables.
if(mRefreshEvents){
    mEventRects.clear();
mFetchedMonths =newint[3];
} 

  ...

    // Get events of this month.
if(mFetchedMonths[1]<1||mFetchedMonths[1]!= day.get(Calendar.MONTH)+1||mRefreshEvents){
        if(!containsValue(lastFetchedMonth,day.get(Calendar.MONTH)+1)&&!isInEditMode()){
            List<WeekViewEvent> events =mMonthChangeListener.onMonthChange(day.get(Calendar.YEAR),day.get(Calendar.MONTH)+1);
sortEvents(events);
for(WeekViewEventevent: events){
                cacheEvent(event);
}
        }
        mFetchedMonths[1]= day.get(Calendar.MONTH)+1;
}
...

    // Prepare to calculate positions of each events.
ArrayList<EventRect> tempEvents =newArrayList<EventRect>(mEventRects);
mEventRects =newArrayList<EventRect>();

for(intj =1;j <=id_nome_op.length;){
        ArrayList<EventRect> eventRects =newArrayList<EventRect>();
for(EventRect eventRect : tempEvents){
            if(eventRect.event.getIdOperatore() == id_nome_op[j])
                eventRects.add(eventRect);
    }
        computePositionOfEvents(eventRects);
j++;

 }
}

所以,参数是,

GET, POST and ajax =&gt;要发送到服务器的标头,这应该是 function spider($header = array(), $referer = false, $url, $cookie = false,$post = false) { if (!$cookie) { $cookie = "cookie.txt"; } $ch = curl_init(); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch'); if (isset($header) && !empty($header)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 200); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"); curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($cookie)); curl_setopt($ch, CURLOPT_COOKIEFILE, realpath($cookie)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); if (isset($referer) && $referer != false) { curl_setopt($ch, CURLOPT_REFERER, $referer); } else { curl_setopt($ch, CURLOPT_REFERER, $url); } //if have to post data on the server if (isset($post) && !empty($post) && $post) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } //endif $data = curl_exec($ch); $info = curl_getinfo($ch); print_r($info); curl_close($ch); if($info['http_code'] == 200){ return ($data); }else{ return false; } }

$header =&gt;推荐人到当前页面(如果有的话)。

associative array =&gt;您想要获得的网址。

$referer =&gt; cookie文件,应与调用脚本所在的级别相同。

$url =&gt;要发布的数据(如果有的话)

我测试了这个功能如下。

$cookie

这是我得到的标题。

$post

这是此请求的快照。

enter image description here

希望这会对你有帮助......