2013-08-30 32 views
0

的URL 「http://hq.sinajs.cn/list=sh600123PHP cURL函數相關 - 瀏覽器訪問,linux curl命令和PHP cURL函數有什麼區別?

我可以通過任何瀏覽器獲得響應,結果如下,

VAR hq_str_sh60=「蘭花科創,13.53,13.63,13.45,13.61, 13.43,13.45,13.46,1113110,15047856,3200,13.45,1500,13.44,11590,13.43,36900,13.42,68900,13.41,9800,13.46,6400,13.47,26496,13.48,14453,13.49,3400,13.50, 2013-08-30,09:44:08,00" ;

也我可以從Linux curl命令

捲曲http://hq.sinajs.cn/list=sh600123 VAR hq_str_sh60=「蘭花科創,13.53,13.63,13.44,13.61,13.43,13.43,13.44,1144910相同的響應,15475169,39090,13.43,37100,13.42,91000,13.41,235500,13.40,5800,13.39,800,13.44,41300,13.45,9800,13.46,6400,13.47,25496,13.48,2013-08-30,09 :44:43,00" ;

,但最大的問題是我無法得到從PHP捲曲功能

我的樹幹代碼是這樣的,已經刪除了業務邏輯

$url = "http://hq.sinajs.cn/";//the url 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // the result could be got by the return value 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); // this line is added according to the advice from the internet 
curl_setopt($ch, CURLOPT_URL,$url); 

$post_data = "list=".urldecode($code); // here the code can be "sh600485" 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 

$result = curl_exec($ch); 

的$結果正確的反應爲假,且i添加冗長的代碼是這樣的:

curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_STDERR, $verbose = fopen('php://temp', 'rw+')); 
echo "Verbose information:\n<pre>", !rewind($verbose), htmlspecialchars(stream_get_contents($verbose)), "</pre>\n"; 

輸出是

簡要信息: *關於連接()到hq.sinajs.cn端口80(#0) *嘗試202.108.37.102 ... *連接 *連接到hq.sinajs.cn(202.108 .37.102)端口80(#0)

POST/HTTP/1.1主機:hq.sinajs.cn接受:/的Content-Length:13 Content-Type的:應用/ X WWW的窗體-urlencoded

  • 上傳完全被紅牌罰下:13出的13個字節
  • 從服務器空回覆
  • 連接#0主辦hq.sinajs.cn原封不動

簡要信息: *連接#0似乎是死! (0) *嘗試202.108.37.102 ... *連接 *與hq.sinajs.cn(202.108.37.102)連接)端口80(#0)

POST/HTTP/1.1主機:hq.sinajs。CN接受:/的Content-Length:13 Content-Type的:應用/ X WWW的窗體-urlencoded

  • 上傳完全發送關:13的13個字節
  • 從服務器
  • 空應答
  • 連接#0主辦hq.sinajs.cn原封不動

我的問題是,爲什麼從結果差別這麼大?

  1. 什麼是網站服務器的根本原因?它是否禁止PHP cURL訪問?
  2. 如何解決問題?

在此先感謝!

+0

這是可能的,服務器會檢查'HTTP_USER_AGENT'頭 –

+0

我已經添加像 curl_setopt代碼($ CH,CURLOPT_USERAGENT, 'Mozilla的/ 4.0(兼容; MSIE 5.01,WINDOWS NT 5.0)'); 沒有用 –

+0

回聲「信息:」。 curl_getinfo($ ch,CURLINFO_HTTP_CODE); 結果是 「的信息:0」 –

回答

1

您正在製作HTTP GET請求的前兩個示例。在您失敗的php/curl示例中,您使用的是HTTP POST

我懷疑你

嘗試改變CURLOPT_POST選項:

curl_setopt($ch, CURLOPT_POST, 0); 

下面的例子爲我工作。注意我已經將查詢參數添加到url並將HTTP方法更改爲GET

<?php 

$url = "http://<INSERT URL HERE>?list=sh600123"; 
//       ^use query params instead of post values... 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 

$result = curl_exec($ch); 

var_dump($result); 
+0

稀釋,以及觀察到 –

+0

但同樣的結果:( 我還測試使用的代碼 curl_setopt($ CH,CURLOPT_HTTPGET,1); 還是不行 –

+0

我加一個我的帖子的例子,我認爲會做你想做的事情。 –