简体   繁体   English

服务器未使用Curl POST请求进行响应

[英]Server not responding using Curl POST Request

I am using this code to get the contents of a post request url using php curl 我正在使用此代码使用php curl获取post请求网址的内容

Code looks as below: 代码如下:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
    'barcode' => 'CP021325078TR',
    'security_code' => $capcha2
)
));

 // Send the request & save response to $resp
 $resp = curl_exec($curl);
 // Close request to clear up some resources
 curl_close($curl);

 echo "<pre>";
 var_dump($resp);
 echo "</pre>";

The result doesn't seem to return anything at all. 结果似乎根本不返回任何东西。

What is wrong with this code? 此代码有什么问题?

Try this: 尝试这个:

$url = 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php';
$postvals = array(
    'barcode' => 'CP021325078TR',
    'security_code' => $capcha2
);
$resp = Request($url,$postvals);
echo "<pre>"; var_dump($resp); exit;

... ...

function Request($url,$params=array()){
        $ch = curl_init();
        $curlOpts = array(
            CURLOPT_URL             => $url,
            CURLOPT_USERAGENT       => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_FOLLOWLOCATION  => true
            );
        if(!empty($params)){
            $curlOpts[CURLOPT_POST] = true;
            $curlOpts[CURLOPT_POSTFIELDS] = $params;
        }
        curl_setopt_array($ch,$curlOpts);
        $answer = curl_exec($ch);
        if (curl_error($ch)) {
            echo curl_error($ch); exit;
        }
        curl_close($ch);
        return $answer;
    }

EDIT: 编辑:

I tested this and got: 我对此进行了测试,并得到:

Could not resolve host: www1.ptt.gov.tr

So make sure you're calling the right endpoint. 因此,请确保您正在调用正确的端点。

Actually you need to set this variable 其实你需要设置这个变量

 $captcha2

To use it here - 在这里使用它-

'security_code' => $capcha2

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

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