简体   繁体   English

将HTTP Post请求发送到Body中包含数组的站点

[英]Sending HTTP Post Request to site with array in Body

I'm trying to make a POST request and send some values in the body of an API call. 我正在尝试发出POST请求并在API调用的主体中发送一些值。 In the documentation of the API it says I need to make a POST request, using startUrls as an array with key and value . API文档中,它说我需要发出一个POST请求,使用startUrls作为一个带keyvalue的数组。

<?php
$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID';

$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);
$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/json',
        'body' => json_encode($postData)
        )
));
$resp = file_get_contents($url, FALSE, $context);
print_r($resp); 
?>

The JSON seems to be how it should, but the script is not sending the body properly to the website. JSON似乎应该如何,但脚本没有正确地将正文发送到网站。

According to the documentation , there is no body option for the HTTP context. 根据文档 ,HTTP上下文没有body选项。 Try content instead: 尝试content

<?php
$url = "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID";

$postData = [
    "startUrls" => [
        ["key"=>"START", "value" => "https://instagram.com/instagram"]
    ]
];

$context = stream_context_create([
    "http" => [
        "method"  => "POST",
        "header"  => "Content-type: application/json",
        "content" => json_encode($postData)
    ]
]);
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);

The following code will work. 以下代码将起作用。 I have set the headers and specified the content type. 我已设置标头并指定了内容类型。

$request = new HttpRequest();
$request->setUrl('$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/x-www-form-urlencoded'
));

$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array('key'=>'START', 'value'=>'https://instagram.com/instagram')
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

If you want to try with cUrl the following code snippet will work. 如果您想尝试使用cUrl,以下代码段将起作用。

$curl = curl_init();
$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID",
            CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => $postData,
                CURLOPT_HTTPHEADER => array(
                    "Cache-Control: no-cache",
                    "content-type: multipart/form-data;"
                ),
            ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);

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

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