0

I'm trying to call an API (mapquest) url from a shared host.

The url works fine, as it shows the expected JSON response when pasted in a browser.

However, I can't make it work from a php page. I tried both curl and file_get_contents with no success.

I keep on getting HTTP/1.1 400 Bad Request error...

Here is the code I use, which is quite basic...

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
    $result=curl_exec($ch);
    if(!$result){
        exit ('cURL ERROR: '.curl_error($ch));
    }
    return var_dump($result);
Yako
  • 3,405
  • 9
  • 41
  • 71

3 Answers3

1

Hello try some thing like this

$query = urlencode('where={"steps":9243}');
$ch = curl_init('https://api.parse.com/1/classes/Steps?'.$query);

curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id: myApplicationID',
    'X-Parse-REST-API-Key: myRestAPIKey',
    'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_exec($ch);
curl_close($ch);

refer this link

Querying API through Curl/PHP

Community
  • 1
  • 1
kailash sharma
  • 356
  • 1
  • 12
0

I got it!

The url was composed of JSON data with spaces.

This part of the URL must be encoded with urlencode(), but not the full url.

It works both with curl and file_get_contents().

Yako
  • 3,405
  • 9
  • 41
  • 71
0

Try below code

    $ch = curl_init();
    $curlConfig = array(
        CURLOPT_URL            => "http://www.bing.com/",
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS     => array(
            'field1' => 'some date',
            'field2' => 'some other data',
        )
    );
    curl_setopt_array($ch, $curlConfig);
    echo $result = curl_exec($ch);
    curl_close($ch);
sandeep soni
  • 303
  • 1
  • 12