Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP cURL函数相关-浏览器访问、linux cURL命令和PHP cURL函数之间有什么区别?_Php_Http_Post_Curl_Webserver - Fatal编程技术网

PHP cURL函数相关-浏览器访问、linux cURL命令和PHP cURL函数之间有什么区别?

PHP cURL函数相关-浏览器访问、linux cURL命令和PHP cURL函数之间有什么区别?,php,http,post,curl,webserver,Php,Http,Post,Curl,Webserver,对于url“” 我可以通过任何浏览器获得响应,结果如下 var hq_str_sh600123=”兰花科创,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

对于url“”

我可以通过任何浏览器获得响应,结果如下

var hq_str_sh600123=”兰花科创,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";

我还可以从LinuxCurl命令获得相同的响应

卷曲 var hq_str_sh600123=”兰花科创,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 cURL函数中得到正确的响应

我的主干代码是这样的,删除了业务逻辑

 $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);
$result为false,我添加了如下详细代码:

    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";
curl\u setopt($ch,CURLOPT\u VERBOSE,1);
curl\u setopt($ch,CURLOPT\u STDERR,$verbose=fopen($php://temp","rw+",;
回显“详细信息:\n”!回放($verbose),htmlspecialchars(流\u获取\u内容($verbose)),“\n”;
curl_setopt($ch, CURLOPT_POST, 0);
输出是

详细信息: *即将连接()到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长度:13内容类型:application/x-www-form-urlencoded

  • 完全发送上载:13个字节中的13个
  • 来自服务器的空回复
  • 0到主机hq.sinajs.cn的连接保持不变
详细信息: *连接#0似乎死了! *正在关闭连接#0 *即将连接()到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长度:13内容类型:application/x-www-form-urlencoded

  • 完全发送上载:13个字节中的13个
  • 来自服务器的空回复
  • 0到主机hq.sinajs.cn的连接保持不变
我的问题是,为什么与结果的差异如此之大

  • 网站服务器崩溃的根本原因是什么?它是否禁止PHP cURL访问
  • 如何解决这个问题

  • 提前谢谢

    您的前两个示例是进行HTTP
    GET
    请求。在失败的php/curl示例中,您使用的是HTTP
    POST

    我怀疑你

    尝试更改
    CURLOPT_POST
    选项:

    <?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);
    
    下面的例子对我很有用。注意,我已将查询参数添加到url,并将HTTP方法更改为
    GET


    服务器可能会检查
    HTTP_USER_AGENT
    headeri,我已经添加了类似curl_setopt的代码($ch,CURLOPT_USERAGENT,'Mozilla/4.0(兼容;MSIE 5.01;Windows NT 5.0)');没有可用的Echo“信息:”。curl_getinfo($ch,CURLINFO_HTTP_代码);结果是“Info:0”,但结果是一样的:(我还使用代码curl_setopt($ch,CURLOPT_HTTPGET,1)进行测试);仍然不起作用我在我的帖子中添加了一个示例,我认为它可以满足您的要求。