[英]Php Curl HTTP/1.1 400 Bad Request
我嘗試將請求(Post Request)發送到url,但返回
HTTP/1.1 400 Bad Request
content-disposition: attachment; filename=json.json
content-length: 118
content-type: application/json;charset=utf-8
date: Sat, 02 Sep 2017 11:33:56 GMT
server: tsa_o
strict-transport-security: max-age=631138519
vary: Origin
x-connection-hash: 32931e6376249fb1689df3203e847a2b
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: 108
x-transaction: 0021b7c8005c6a4e
x-tsa-request-body-time: 1
x-xss-protection: 1; mode=block
{“ capsError”:“權限不足”,“ requestId”:“ 4f0c87b1349996812fafe51ca6a8197b5bb1e9d560d7dcb8a450ab 3c890e40ac”}
我的代碼
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $URL,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => 'postField',
CURLOPT_COOKIEJAR => realpath("cookies/" . $name . ".txt"),
CURLOPT_COOKIEFILE => realpath("cookies/" . $name . ".txt"),
CURLOPT_CONNECTTIMEOUT=>30,
CURLOPT_HEADER => true, // return headers
CURLOPT_SSL_VERIFYPEER=>true,
));
$output = curl_exec($ch);
curl_close( $ch );
return $output;
我肯定來自所有帖子字段,但返回的響應相同,但在瀏覽器中一切正常
首先,您可以更改$output = curl_exec($ch);
echo $output = curl_exec($ch);
這樣,您將在瀏覽器中看到錯誤消息。
另外,嘗試urlencode($ url); 如果您的網址中有一些空格。
但是您的問題很可能是目標服務器的安全性。很多服務器將cURL視為跨站點偽造攻擊,這就是為什么任何cURL腳本都必須在其post參數中包含目標頁面的csrf令牌。
您必須首先從目標頁面找到該令牌,然后編寫代碼,以便您的腳本將從curl_exec輸出字符串中解析該令牌。
您始終可以重寫代碼。
像這樣的東西:
$http_headers = array(
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',);
$crl = curl_init();
$url = "http://www.example.gr/node/add/content";
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt ($crl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_HEADER, true);
curl_setopt($crl, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($crl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36");
curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata);
echo $result=curl_exec($crl);
curl_setopt($crl, CURLOPT_POST, 1);
$postdata = array(
"title_field[und][0][value]"=>"category",
"op"=>"Save", );
curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata);
echo $result=curl_exec($crl);
您的版本可能出了很多問題。如果這樣不起作用,請重新檢查我在第一批答案中寫的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.