如何在这些参数上实现cURL

时间:2018-06-05 13:26:09

标签: php curl

我需要你的帮助。

我正在研究Prepostseo的剽窃api,我已经使用cURL调用了这个参数。现在,我对cURL知之甚少,因为我一直在使用file_get_contents。但现在我只需要使用cURL。我搜索了他们的文档,没有可用的参考资料或源代码,甚至没有在Github上。

以下是有关如何实现此问题的参数,我需要帮助:

curl -X POST https://www.prepostseo.com/apis/checkSentence \ 
-d "key=YOUR_KEY" 
-d "query=Inside that cage there was a green teddy bear" 

提前致谢!

2 个答案:

答案 0 :(得分:0)

为了将来参考,您可以使用https://incarnate.github.io/curl-to-php

<?php
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://www.prepostseo.com/apis/checkSentence");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "key=YOUR_KEY&query=Inside that cage there was a green teddy bear");
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }

    curl_close ($ch);

    echo $result;
?>

答案 1 :(得分:0)

This link解释了如何在PHP中使用cURL时需要了解的所有内容。

下面的代码段将POST通过指定网址的URL编码查询字符串。

执行cURL调用时,响应会分配给$respsonse变量,之后会关闭cURL调用。

$payload = [
  'key' => 'YOUR_KEY',
  'query' = 'Inside that cage there was a green teddy bear'
];    

$url = "https://www.prepostseo.com/apis/checkSentence";

//set up cURL - below is a general basic set up
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

//specify your method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

//for the body values you wish to POST through
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload)); 

//specifiy any specific headers you need here in your array
curl_setopt($ch, CURLOPT_HTTPHEADER, []);

//execute and close cURL
$response = curl_exec($ch);
curl_close($ch);