2

I'm trying to make a POST request and send some values in the body of an API call. In the documentation of the API it says I need to make a POST request, using startUrls as an array with key and value.

<?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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Jack Ellis
  • 29
  • 1
  • 1
  • 6
  • Why do you say that the body is not properly sent? Do you have access to the server's logs? What is the value of $resp? – floverdevel Apr 06 '18 at 19:58
  • 1. Because it doesn't start the crawler on Apify with changed URL. 2. No, we just know it doesn't start it right, but if done with POST from console, it works. 3. https://justpaste.it/1jabp – Jack Ellis Apr 06 '18 at 20:22

2 Answers2

2

According to the documentation, there is no body option for the HTTP context. Try content instead:

<?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);
miken32
  • 42,008
  • 16
  • 111
  • 154
1

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_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);
Amit Kumar
  • 452
  • 4
  • 10