当前位置: 代码迷 >> PHP >> curl post异常,求解
  详细解决方案

curl post异常,求解

热度:105   发布时间:2016-04-28 19:58:02.0
curl post错误,求解
$url="http://dct.dhl.com/input.jsp?langId=cn&originCCId=CN";
$post_data="orgCtry=CHINA, PEOPLES REPUBLIC (CN)&orgZip=510011&orgCity=GUANGZHOU&dstCtry=HONG KONG (HK)&dstCity=HONG KONG&declValCur=USD&weight=0.2&shipping_options=Submit";
$ch=curl_init();
echo phpinfo();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'); 
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
//ob_start();
$output=curl_exec($ch);
curl_close($ch);
print_r($output);
//ob_end_flush();

不知道为什么post不成功啊,求救

------解决方案--------------------
// 检查是否有错误发生
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}
------解决方案--------------------
$url="http://dct.dhl.com/input.jsp?langId=cn&originCCId=CN";
print_r(get_headers($url));
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Sat, 11 Jan 2014 02:44:34 GMT
    [2] => Content-Length: 10641
    [3] => Content-Type: text/html; charset=UTF-8
    [4] => X-Powered-By: Servlet/2.5 JSP/2.1
    [5] => Set-Cookie: JSESSIONID=pmT2SQwS3QSTgbTCNygyJlPjvC3hKmkgHDyJYQys145vGnvmBpbP!1824887213; path=/; HttpOnly
    [6] => Set-Cookie: BIGipServerdct_ratio=627591333.43295.0000; path=/
    [7] => Via: 1.0 dct.dhl.com
    [8] => Connection: close
    [9] => Set-Cookie: TSe13065=c415deb6fcd5ecb861ff24a0e0fc22856d2ecfd394ca9e6352d0b01160ac0ec57b9c5c10beb8cf9b0458f95b; Path=/
)
该站使用了 session 和 stoke 而你却没有
自然就模拟失败了
------解决方案--------------------
引用:
Quote: 引用:

$url="http://dct.dhl.com/input.jsp?langId=cn&originCCId=CN";
print_r(get_headers($url));
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Sat, 11 Jan 2014 02:44:34 GMT
    [2] => Content-Length: 10641
    [3] => Content-Type: text/html; charset=UTF-8
    [4] => X-Powered-By: Servlet/2.5 JSP/2.1
    [5] => Set-Cookie: JSESSIONID=pmT2SQwS3QSTgbTCNygyJlPjvC3hKmkgHDyJYQys145vGnvmBpbP!1824887213; path=/; HttpOnly
    [6] => Set-Cookie: BIGipServerdct_ratio=627591333.43295.0000; path=/
    [7] => Via: 1.0 dct.dhl.com
    [8] => Connection: close
    [9] => Set-Cookie: TSe13065=c415deb6fcd5ecb861ff24a0e0fc22856d2ecfd394ca9e6352d0b01160ac0ec57b9c5c10beb8cf9b0458f95b; Path=/
)
该站使用了 session 和 stoke 而你却没有
自然就模拟失败了

加上了cookies,不知怎么加session,大牛帮我看看啊,谢谢
$cookie_file = dirname(__FILE__).'/cookie.txt';
$url="http://dct.dhl.com/input.jsp?langId=cn&originCCId=CN";

$post_data="orgCtry=CHINA, PEOPLES REPUBLIC (CN)&orgZip=510011&orgCity=GUANGZHOU&dstCtry=HONG KONG (HK)&dstCity=HONG KONG&declValCur=USD&weight=0.2&shipping_options=Submit";

$curl = curl_init($url); //初始化
curl_setopt($curl, CURLOPT_HEADER, 0); //不返回header部分
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出
curl_setopt($curl, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
curl_exec($curl);
curl_close($curl);

$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  相关解决方案