I run into strange problem with GoogleMaps API.
Calling the following url in browser, everything works fine.
https://maps.googleapis.com/maps/api/geocode/json?components=country:DE|postal_code:81823&key=[mykey]
But using same url in PHP with curl I get a ZERO_RESULT?
Calling https://maps.googleapis.com/maps/api/geocode/json?components=country:DE|postal_code:90471 it works fine in PHP and Browser.
My code in PHP:
$timeout = 5;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
$decode = json_decode( curl_exec( $ch ) );
curl_close( $ch );
print_r($decode);
returns with the first url
stdClass Object
(
[results] => Array
(
)
[status] => ZERO_RESULTS
)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 15 days ago.
Improve this question
How do I fix this issue? I cannot send sms on my work.
$ch = curl_init(); //curl()
$parameters = array(
'apikey' => '', //Your API KEY
'number' => '',//number
'message' => 'I just sent my first message with Semaphore', //message
'sendername' => 'SEMAPHORE');
curl_setopt( $ch, CURLOPT_URL,'https://semaphore.co/api/v4/messages');
curl_setopt( $ch, CURLOPT_POST, 1 );
//Send the parameters set above with the request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $parameters ) );
How do I fix this issue? I have my apikey in semaphore.
Hey i tried this code and print output
<?php
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
$ch = curl_init();
$parameters = array(
'apikey' => '', //Your API KEY
'number' => '0987654321',//for testing no
'message' => 'I just sent my first message with Semaphore',
'sendername' => 'SEMAPHORE'
);
curl_setopt( $ch, CURLOPT_URL,'https://semaphore.co/api/v4/messages' );
curl_setopt( $ch, CURLOPT_POST, 1 );
//Send the parameters set above with the request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $parameters ) );
// Receive response from server
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close ($ch);
//Show the server response
echo $output;
i got return output
"message_id":73840803,
"status":"Pending",
Using live key and test and try to hit same code check status is pending to received
This worked for me
$ch = curl_init();
$parameters = array(
'apikey' => '',
'number' => '9000000000',
'message' => 'I just sent my first message with Semaphore',
'sendername' => 'SEMAPHORE'
);
curl_setopt( $ch, CURLOPT_URL,'https://semaphore.co/api/v4/messages' );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//Send the parameters set above with the request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $parameters ) );
$result = curl_exec($ch);
print_r($result);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
print_r($error_msg);
}
curl_close($ch);
exit;
I'm trying to use the gfycat API to create a gfycat with a file upload through curl with php but it doesn't work and var_dump($response) gives me bool(false).
My Code:
$file_path = $target_dir.$newfilename;
$cFile = curl_file_create($file_path);
$data = array(
"file" => $cFile,
);
$target_url = "https://filedrop.gfycat.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data"
));
$response = curl_exec($ch);
var_dump($response); // bool(false) here
curl_close($ch);
Help would be really appreciated. Thanks.
Btw I got it to work with this:
$file_path = "ABSOLUTE_FILE_PATH".$newfilename;
$cFile = curl_file_create(realpath($file_path));
$data = array(
"key" => $newfilename,
"file" => $cFile,
);
$target_url = "https://filedrop.gfycat.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data"
));
$response = curl_exec($ch);
curl_close($ch);
when debugging curl code, it's often a good idea to enable CURLOPT_VERBOSE and check the stderr log. further more, if curl_exec reutrned bool(false), it means there was a problem with the transfer, and you can use the curl_error() function to get an error message. and lastly, don't set the header "Content-Type: multipart/form-data" manually, curl will set that header for you, and unlike you, curl won't make any typos in doing so, and worse, you risk overwriting/removing the boundary parameter of the header.
try
$file_path = $target_dir . $newfilename;
$cFile = curl_file_create ( $file_path );
$data = array (
"file" => $cFile
);
$target_url = "https://filedrop.gfycat.com";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $target_url );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
try {
$stderrh = tmpfile ();
curl_setopt_array ( $ch, array (
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => $stderrh
) );
$response = curl_exec ( $ch );
if ($response === false) {
throw new \RuntimeException ( "curl error " . curl_errno ( $ch ) . ": " . curl_error ( $ch ) . " - verbose log: " . file_get_contents ( stream_get_meta_data ( $stderrh ) ['uri'] ) ); // https://bugs.php.net/bug.php?id=76268
}
} finally{
curl_setopt_array ( $ch, array (
CURLOPT_VERBOSE => 0,
CURLOPT_STDERR => STDERR
) );
fclose ( $stderrh );
}
var_dump ( $response ); // bool(false) here
curl_close ( $ch );
now, if there is an error, it should give you a nice detailed log of what happened up to to the curl error, in the exception error log.
This is the extremely vague response I'm getting, while trying to get a user access token:
{
"error":"invalid_request",
"error_description":"request is invalid",
"error_uri":null
}
Here is my code:
$headers = array (
"Authorization: ".sprintf(
'Basic %s',
base64_encode(sprintf('%s:%s', $client_id, $client_secret))
)." ",
'Content-Type:application/x-www-form-urlencoded'
);
$apiURL = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
$urlParams = array (
"grant_type" => "authorization_code",
"code" => $auth_code,
"redirect_uri" => "xxx-xxx-xxx-SBX-ccd-xxx"
);
$data_json = json_encode($urlParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 1 );
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data_json );
$resp = curl_exec ( $ch );
curl_close ( $ch );
print_r ( $resp );
How can I debug this when it doesn't even hint at what's wrong with my request?
I've been through this page step-by-step:
https://developer.ebay.com/devzone/rest/ebay-rest/content/oauth-gen-user-token.html#Updating
It seems that you encode your body in Json while the authorization server expects body parameters in POST field format.
The headers should also contains key/value pairs but you are sending only a list of values
Could you please try with the following code:
$headers = array (
'Authorization' => sprintf('Basic %s',base64_encode(sprintf('%s:%s', $client_id, $client_secret))),
'Content-Type' => 'application/x-www-form-urlencoded'
);
$apiURL = "https://api.sandbox.ebay.com/identity/v1/oauth2/token";
$urlParams = array (
"grant_type" => "authorization_code",
"code" => $auth_code,
"redirect_uri" => "xxx-xxx-xxx-SBX-ccd-xxx"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Should be removed on production
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 1 );
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $urlParams );
$resp = curl_exec ( $ch );
curl_close ( $ch );
print_r ( $resp );
Authorization header should have a following format:
'Authorization' => sprintf('Basic <%s>',base64_encode(sprintf('%s:%s', $client_id, $client_secret)))
I am trying to use cloudsight API (http://cloudsight.readme.io/v1.0/docs) that requires me to use both POST and GET. I've never used a REST API before but after doing some research found that to POST using PHP would work.
I found the following code in the api documentation but am not sure how to convert this command line curl to PHP. The response is in JSON.
curl -i -X POST \
-H "Authorization: CloudSight [key]" \
-F "image_request[image]=#Image.jpg" \
-F "image_request[locale]=en-US" \
https://api.cloudsightapi.com/image_requests
curl -i \
-H "Authorization: CloudSight [key]" \
https://api.cloudsightapi.com/image_responses/[token]
If you're still interesting by the answer :
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://api.cloudsightapi.com/image_requests" );
$postFields = array(
'image_request' => array(
'remote_image_url' => $url,
'locale' => 'en-US'
)
);
$fields_string = http_build_query($postFields);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields_string );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: CloudSight [key]', "Content-Type:multipart/form-data" ) );
curl_exec( $ch );
curl_close( $ch );
If using the php curl library, you can do this for the POST:
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://api.cloudsightapi.com/image_requests" );
$postFields = array(
'image_request' => array(
'image' => '#/path/to/image.jpeg',
'locale' => 'en-US'
)
);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: CloudSight [key]' ) );
curl_exec( $ch );
curl_close( $ch );
PHP>=5.5 also provides a CURLFile class (http://php.net/manual/en/class.curlfile.php) for working with files instead of passing the path, as in the example above.
For the GET, you can just remove these two lines and alter the url:
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields );
Another option would be to use Guzzle if you use Composer in your project ( http://guzzle.readthedocs.org/en/latest/).
i try to delete photos i have uploaded with app by php
the result of upload example :
{"id":"429774393794352","post_id":"276744849097308_429774413794350"}
first which one id or post_id will be use
and how to select it with php ??
and how to make order to delete it with php code by curl library
my idea is , i will store all photos ids into sql to delete it any time
my upload code i used
$file='./'.$new_string;
$args = array(
'message' => $message,
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/album id /photos?access_token='.$accsesss;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
i try
$args = array(
'id' => '276744849097308_429774413794350',
'access_token' => $accsesss ,
);
$ch = curl_init();
$url = 'https://graph.facebook.com/276744849097308_429774413794350?method=DELETE&access_token=$accsesss';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); $a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
result
Array ( [error] => Array ( [message] => Invalid OAuth access token. [type] => OAuthException [code] => 190 ) )
$Curl_Session = curl_init('https://graph.facebook.com/429774393794352');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "method=DELETE&access_token=$masteracsess");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
echo $a8=curl_exec ($Curl_Session);
curl_close ($Curl_Session);
thanks for your help #CBroe