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);
2
  • Opps you didn't set header.. Commented Mar 31, 2015 at 10:21
  • 1
    Are you sending all necessary headers? Commented Mar 31, 2015 at 10:21

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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().

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.