Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl is not sending on my localserver

Tags:

php

curl

I am using below code to get user data but don't know why it saying invalid token error while i am using same code on another server and its working fine. I checked CURL is enabled in my system. You can see working example with below code on http://specificpromotions.com/test/test.php. What could be the problem please help me. Your help would be appreciated. thanks

$ch = curl_init();
//you will need to change the 'eric' to what the user is searching for in the manual entry section
$data = array('qFunc' => 'searchActiveStaff', 'qArgs' => 'eric');

curl_setopt($ch,CURLOPT_URL,'http://api.yfcimpact.com/yfc_dev.php/api/usa/person?' . http_build_query($data) );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERPWD, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJob3N0IjoiMTIwLjU5LjE1OS4yMzIiLCJ1c2VybmFtZSI6InJzbWl0aHR5MyIsImlkIjoxMDAwNTQwMjcwLCJpYXQiOjE0MzI4Nzk0NjQsImV4cCI6MTQzMjkwMTA2NCwicm9sZXMiOlsiUk9MRV9VU0VSIiwiUk9MRV9zZXR0aW5ncyIsIlJPTEVfcGVvcGxlIiwiUk9MRV9wZW9wbGVfYWRkX25ldyIsIlJPTEVfcGVvcGxlX2VkaXQiLCJST0xFX3BlcnNvbl9lZGl0X3Blcm1pc3Npb24iLCJST0xFX3NldHRpbmdzX3Blcm1pc3Npb25zIiwiUk9MRV9zZXR0aW5nc19jdXN0b21fZmllbGRzIiwiUk9MRV9vZmZpY2UiLCJST0xFX3NldHRpbmdzX21pbmlzdHJ5X3NpdGVzIiwiUk9MRV9zZXR0aW5nc19taW5pc3RyeV9zaXRlc19hZGQiLCJST0xFX3NldHRpbmdzX2xlZ2FsIiwiUk9MRV9wZW9wbGVfZWRpdF9saXN0IiwiUk9MRV9vZmZpY2VfZmluYW5jaWFsIiwiUk9MRV9vZmZpY2VfYnVkZ2V0X3BsYW4iLCJST0xFX29mZmljZV9taW5zdGF0cyIsIlJPTEVfc2V0dGluZ3NfZmllbGRfbGFiZWxzIiwiUk9MRV9vZmZpY2VfYmdjaGVjayIsIlJPTEVfb2ZmaWNlX3ZlaGljbGUiLCJST0xFX29mZmljZV93b3JrZXJzY29tcCIsIlJPTEVfc2V0dGluZ3NfY3VzdG9tX2ZpZWxkX2dyb3VwcyIsIlJPTEVfc2V0dGluZ3NfdGV4dCIsIlJPTEVfc2V0dGluZ3NfZW1haWwiLCJST0xFX29mZmljZV9jaGFydGVyaW5nIiwiUk9MRV9ldmVudHMiLCJST0xFX3NldHRpbmdfY3VzdG9tcXVlc3Rpb25zIl0sImZ1bGxOYW1lIjoiUm9nZXIgU21pdGgiLCJjb3JlT3JnSWQiOiI0MjcifQ.V9B-TE_72D0YYE5FAHPB_pkYGhkAGiHRQzZjTEb0Jag:" );

$json = curl_exec($ch);
$people = json_decode($json);

print "people who have eric in their name:\n";
var_export($people);
like image 495
Vinie Avatar asked May 29 '15 06:05

Vinie


People also ask

Why is curl not working on my localhost?

Most likely, curl is not able to resolve localhost. It switches to an IPv6 address of localhost and your couchdb is not listening for an IPv6 address. Change from localhost to 127.0.0.1. Thanks for contributing an answer to Ask Ubuntu!

Why is curl not working on my Machine?

If that is the case then you need to look at the thing that curl is trying to connect to, and make sure it is listening on all the addresses associated with the machine, or at least the one for Load-testing-1-4. Using 0.0.0.0 as the address will usually do this for IPv4 rather than an explicit address such as 127.0.0.1.

What port does curl use by default?

Curl by default tries to use port 1080, which is probably not open on your localhost / router / ISP. Share Follow answered Dec 7 '11 at 17:33 Dan CrewsDan Crews

How do I install cURL library in PHP without curling?

You can have the curllibrary installed with PHP without actually having the curlbinary on your system. Drop to a shell prompt and type curl. (If it's standard on OSX then please forgive my ignorance - I'm not a Mac guy)


1 Answers

Firstly check if the curl is enabled in your system or server. You can check it by using a simple page with this code.

<?php 
 phpinfo();
?>

If curl is enabled it will show there. If its not enabled enable it. You can check this link for that.

Next check for the curl errors and clear it. Just add this code after the curl_exec($ch);

    $ch = curl_init();
    $data = array('qFunc' => 'searchActiveStaff', 'qArgs' => 'eric');
    curl_setopt($ch,CURLOPT_URL,'http://api.yfcimpact.com/yfc_dev.php/api/usa/person?' . http_build_query($data) );
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_USERPWD, "ag:" );    
    if(curl_exec($ch) === false)
{
    echo 'Error Details: ' . curl_error($ch);
}
else
{
    echo 'Success';
}
like image 56
Darshan Avatar answered Sep 29 '22 00:09

Darshan