简体   繁体   English

cURL http请求未从服务器返回任何响应,但fiddler或firebug网络工具会返回

[英]cURL http request returns no response from server but fiddler or firebug network tool does

I can send HTTP request via fiddler or firebug network tool and receive server response but I cannot receive the same response via cURL and php; 我可以通过提琴手或萤火虫网络工具发送HTTP请求并接收服务器响应,但不能通过cURL和php接收相同的响应; What is wrong? 怎么了?

I opened the URL ( http://search.icbar.org ) in my browser and searched with a filled "lawyer license number" field with "102", I caught the http request and then wrote the code below to imitate the action once more, but there was no response from server at all. 我在浏览器中打开了URL( http://search.icbar.org ),并使用“ 102”填充了“律师执照号码”字段,并捕获了http请求,然后编写了下面的代码来模拟一次操作更多信息,但服务器完全没有响应。

my code is here: 我的代码在这里:

<?php
//echo phpinfo();
// Get cURL resource
$curl = curl_init();

// Set some options - we are passing in a useragent too here
$pf=array('name' => '','family' => '','licenseNumber' => '102','Gender' => '','workState' => '','Province' => '','address' => '');

$headers = array(
    'Accept-Language: en-US,en;q=0.5',
    'Accept-Encoding: gzip, deflate',
    'Cache-Control: no-cache',
    'Content-Type: application/json',
    'X-Requested-With: XMLHttpRequest',
    'Pragma: no-cache',
    'Content-Length: 99',
    'Connection: keep-alive',
    'Host: search.icbar.org',
    'Referer: http://search.icbar.org/',
    'User-Agent: me');

curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, 'http://search.icbar.org/Handler/Law.ashx?Method=mGetLawyers');
curl_setopt($curl, CURLOPT_USERAGENT, 'ali');
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pf);

// Send the request & save response to $resp
$resp=curl_exec($curl);
print_r(json_decode($resp));

if(!$resp)
{
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

// Close request to clear up some resources
curl_close($curl);

?>

当您向服务器发送json请求时,将数组转换为json对象。

$pf = json_encode($pf);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM